﻿/*
 * Created by SharpDevelop.
 * User: XPMUser
 * Date: 11/02/2011
 * Time: 13:01
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace KDG.FoldersSize4
{
	/// <summary>
	/// Description of FindViewModel.
	/// </summary>
	public class FindViewModel : ViewModelBase
	{
		private List<string> _findTemplates = null;
		FoldersSizeModel _model = null;
		public FindSummary FindSummary { get; set; }
		public FindViewModel()
		{
			_findTemplates = new List<string>
			{
				"*.doc*",
				"*.dll",
				"*.exe",
				"*.txt"
			};
			
			FindItems = new ObservableCollection<ScannerItem>();
		}
		public FindViewModel(FoldersSizeModel Model) :this()
		{
			_model = Model;
		}
		
		public List<string> FindTemplates { get { return _findTemplates; } private set { _findTemplates = value; } }
		private RelayCommand _findCommand = null;
		public RelayCommand FindCommand
		{
			get { 
				if(_findCommand==null)
					_findCommand = new RelayCommand( p=>
					                                {
					                                	this.FindItems.Clear();
					                                	OnPropertyChanged("FindItems");
					                                	this.FindSummary = new FindSummary();
					                                	OnPropertyChanged("FindSummary");
					                                	
					                                	string fileMask = CurrentFileMask;
					                                	
					                                	string pattern =
												        '^' + 
												        Regex.Escape(fileMask.Replace(".", "__DOT__")
												                         .Replace("*", "__STAR__")
												                         .Replace("?", "__QM__"))
												            .Replace("__DOT__", "[.]")
												            .Replace("__STAR__", ".*")
												            .Replace("__QM__", ".")
												        + '$';
					                                	
					                                	Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
					                                	
					                                	FindScannerItem(_model.RootItem, r, _findItems);
					                                	OnPropertyChanged("FindSummary");
					                                	
													
					                                	OnPropertyChanged("FindItems");
					                                }, 
					                                c=>
					                                {
					                                	return (
					                                		_model.RootItem!=null && 
					                                		!string.IsNullOrEmpty(this.CurrentFileMask));
					                                }
					                               );
				
				return _findCommand;
			}
		}
		
		ObservableCollection<ScannerItem> FindScannerItem(ScannerItem rootItem, Regex r, ObservableCollection<ScannerItem> findItems)
		{
			if(r.Matches(rootItem.FullName).Count>0)
			{
				findItems.Add(rootItem);
				if(rootItem.IsDirectory)
				{
					FindSummary.Folders++;
				}
				else
				{
					FindSummary.Files++;
					FindSummary.TotalLength += rootItem.Length;
				}
			}
			
			rootItem.Childs.ForEach( c=>FindScannerItem(c, r, findItems) );
			
			return findItems;
		}
		
	
		
		private ObservableCollection<ScannerItem> _findItems = null;
		public ObservableCollection<ScannerItem> FindItems 
		{ 
			get { return _findItems; }
			private set { _findItems = value; }
		}
		
		private string _currentFileMask = string.Empty;
		public string CurrentFileMask
		{
			get { return _currentFileMask; }
			set {
				if(_currentFileMask == value )
					return ;
				
				_currentFileMask=value;
				OnPropertyChanged("CurrentFileMask");
			}
		}
		
		private long _findItemsCount = 0;
		public long FindItemsCount 
		{ 
			get { return _findItemsCount; }
			set
			{
				if(_findItemsCount==value)
					return ;
				
				_findItemsCount = value;
				OnPropertyChanged("FindItemsCount");
			}
				
		}
		private long _findItemsTotalSize = 0;
		public long FindItemsTotalSize 
		{ 
			get { return _findItemsTotalSize;}
			set {
				if(_findItemsTotalSize==value)
					return ;
				
				_findItemsTotalSize = value;
				OnPropertyChanged("FindItemsTotalSize");
			}
		}
		private object _selectedItem = null;
		public object SelectedItem 
		{ 
			get { return _selectedItem; } 
			set {
				if(_selectedItem==value)
					return ;
				
				_selectedItem = value;
				OnPropertyChanged("SelectedItem");
			}
		}
		
		private RelayCommand _openContainingFolderCommand = null;
		public RelayCommand OpenContainingFolderCommand
		{
			get { 
				if(_openContainingFolderCommand==null)
					_openContainingFolderCommand = new RelayCommand( p=>
					                                {
					                                	ScannerItem si = SelectedItem as ScannerItem;
					                                	if (si != null) 
					                                	{
					                                		FileInfo fi = new FileInfo(si.FullName);
					                                		if(fi.Directory.Parent.Exists)
					                                			Process.Start(fi.Directory.Parent.FullName);
					                                	}
					                                }
					                               );
				
				return _openContainingFolderCommand;
			}
		}
		private RelayCommand _openFolderCommand = null;
		public RelayCommand OpenFolderCommand
		{
			get { 
				if(_openFolderCommand==null)
					_openFolderCommand = new RelayCommand( p=>
					                                {
					                                	ScannerItem si = SelectedItem as ScannerItem;
					                                	if (si != null) 
					                                	{
					                                		DirectoryInfo di = new DirectoryInfo(si.FullName);
					                                		if( di.Exists )
					                                			Process.Start(di.FullName);
					                                	}
					                                 }
					                               );
				
				return _openFolderCommand;
			}
		}
	}
}
